﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.Entities.Seguridad;
using GC2.ExceptionHandler;

public partial class Empleados_TareasAdmin : GenericPage
{
    #region Propiedades
    Usuarios usuario = new Usuarios();
    Tareas tareaActual = new Tareas();    
    List<Tareas> listaTareas = new List<Tareas>();
    private ExceptionConstructor exNew = new ExceptionConstructor();
    #endregion

    #region Controles

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                usuario = (Usuarios)Session["UsuarioActual"];
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            BuscarTareas();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtCodigo.Text.Trim() != "")
            {
                tareaActual.Id_Tarea = Convert.ToInt16(txtCodigo.Text.Trim());
            }
            else
            {
                tareaActual.Id_Tarea = 0;
            }

            tareaActual.Tarea = txtTarea.Text.Trim();
            Session["Accion"] = 1;
            Session["Tarea"] = tareaActual;
            Response.Redirect("TareasGestion.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/Seguridad/Default.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvTareas_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvTareas.PageIndex = e.NewPageIndex;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvTareas_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try { }
        catch (Exception ex) { ErrorUI(ex); }
    }

    protected void gvTareas_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            listaTareas = (List<Tareas>)Session["listaTareas"];
            Tareas tarea = new Tareas();
            tarea.Id_Tarea = (int)gvTareas.SelectedDataKey.Value;
            foreach (Tareas item in listaTareas)
	        {
                if (item.Id_Tarea == tarea.Id_Tarea)
                {
                    tarea.Tarea = item.Tarea;
                    break;
                }
	        }
            Session["Tarea"] = tarea;
            Session["Accion"] = 2;
            Response.Redirect("TareasGestion.aspx", false);
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

    #region Métodos
    private void CargarGrilla()
    {
        try
        {
            listaTareas = (List<Tareas>)Session["listaTareas"];
            gvTareas.DataSource = listaTareas;
            gvTareas.DataBind();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    private void BuscarTareas()
    {
        try
        {
            BMTareas bmTareas = new BMTareas(Connection);
            listaTareas = bmTareas.BMTareasGetAll();

            Session["listaTareas"] = listaTareas;
            CargarGrilla();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }

    #endregion

  
}
